package com.songshuai.sales.client.controller;

import com.alibaba.fastjson.JSON;
import com.songshuai.sales.client.service.TestService;
import com.songshuai.webapi.baseapi.model.sales.Sop;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("test")
public class TestController {


    @Autowired
    public TestService testService;

    @RequestMapping("/findSopById")
    public CompletableFuture<QueryResponse> findSopById(String id){
        return CompletableFuture.supplyAsync(()->{
            return testService.selectByIds(id);

        });
    }
    /**
     * 结果转换
     * @param id
     * @return
     */
    @RequestMapping("/findStringById")
    public CompletableFuture<QueryResponse> findStringById(String id){
        return CompletableFuture.supplyAsync(()->
                testService.selectByIds(id)
        ).thenApply(sop->{
            String string = JSON.toJSONString(sop);
            return QueryResponse.setResData(string);
        });
    }

    /**
     * 将两个函数联合的结果    提供给其它函数调用
     * @param id
     * @return
     */
    @RequestMapping("/findCombineById")
    public CompletableFuture<QueryResponse> findCombineById(String id){
        return CompletableFuture.supplyAsync(()->{
            return  "i am here";}
        ).thenCombine(CompletableFuture.supplyAsync(()->
                "haha"
        ),(ret,mes)->ret+mes)
                .thenCompose(p->CompletableFuture.supplyAsync(()->QueryResponse.setResData(p+"and everyone")));
    }
    /**
     * 将第一个函数的结果  传递给第二个函数   然后再返回最后一个函数的执行最终结果
     * @param id
     * @return
     */
    @RequestMapping("/findMapByIds")
    public CompletableFuture<QueryResponse> findMapById(String id){
        return CompletableFuture.supplyAsync(()->
                testService.selectByIds(id)
        ).thenCompose(sop-> CompletableFuture.supplyAsync(()->{
            Map<String,Object> map=new ConcurrentHashMap<>();
            map.put("sop",sop);
            map.put("test","haha");
            return QueryResponse.setResData(map);
        }));
    }


}
